package com.lintf.devdemo.controller;

import com.lintf.devdemo.common.Result;
import com.lintf.devdemo.common.ResultCode;
import com.lintf.devdemo.entity.dto.ExportUserInfoExcelDto;
import com.lintf.devdemo.error.BusinessException;
import com.lintf.devdemo.service.ExcelFileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;

/**
 * @ system:Window
 * @ author:lin tingfeng
 * @ data:2022/11/04-10:11
 * @ package_name:com.lintf.devdemo.controller
 * @ project_name:DevDemo
 * @ desc:
 */
@Api(value = "Excel文件操作API")
@RestController
@RequestMapping("/excelFileOption")
public class ExcelFileController {

    @Autowired
    private ExcelFileService excelFileService;

    /*@PostMapping("/importUserInfoExcel")
    public Result importUserInfoExcel(MultipartHttpServletRequest files) {
        try {
            Iterator<String> iter = files.getFileNames();
            String fileName = "";
            while (iter.hasNext()) {
                fileName = iter.next();
            }
            if (StringUtils.isBlank(fileName)) {
                return Result.failed("请上传文件");
            }
            return excelFileService.importUserInfoExcel(files.getFile(fileName));
        } catch (BusinessException e) {
            return Result.failed(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return Result.failed("导入失败");
        }
    }*/

    /*@PostMapping("/exportUserInfoExcel")
    public Result exportUserInfoExcel(HttpServletRequest request, HttpServletResponse response, @RequestBody ExportUserInfoExcelDto dto) {
        try {
            excelFileService.exportUserInfoExcel(request, response, dto);
            return Result.success(null);
        } catch (BusinessException e) {
            return Result.failed(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return Result.failed("导入失败");
        }
    }*/

    @ApiOperation(value = "导入用户信息Excel表格")
    @PostMapping("/importUserInfoExcel")
    public Result importUserInfoExcel(@RequestPart("file") MultipartFile file) throws Exception {
        try {
            if (StringUtils.isBlank(file.getName())) {
                return Result.failed("请上传文件");
            }
            return excelFileService.importUserInfoExcel(file);
        } catch (BusinessException e) {
            return Result.defineError(e);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.otherError(ResultCode.FAILED);
        }
    }

    @ApiOperation(value = "导出用户信息Excel表格")
    @GetMapping("/exportUserInfoExcel")
    public void exportUserInfoExcel(HttpServletResponse response, ExportUserInfoExcelDto dto) {
        try {
            excelFileService.exportUserInfoExcel(response, dto);
        } catch (BusinessException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
